
#include "AIPlayer.h"
#include "../World.h"
#include "../Map.h"
#include "../GameState.h"

#include <cmath>
#include <iostream>


AIPlayer::AIPlayer(unsigned player_id, std::string player_name, unsigned team_id) :
    Player(player_id, player_name, team_id) {
   
    attacker_role = NULL;
    defender_role = NULL;
    marauder_role = NULL;
 
    spawnNewShip();
}

AIPlayer::~AIPlayer(){
    if(attacker_role != NULL){ delete attacker_role; }
    if(defender_role != NULL){ delete defender_role; }
    if(marauder_role != NULL){ delete marauder_role; }
}

void AIPlayer::update(void){
    checkHealth();
    if(amAlive()){
        last_alive_tick = Timer::currentTicks();
    }
    else if(Timer::currentTicks()-last_alive_tick < 2000){
        return;
    }
    else{
        purgeRoles();
        spawnNewShip();
    }
    
    return;
    //checkForDeath();
    //return;

    Ship *pship = (Ship*)(my_ship_ref.getPointer());
    if(pship == NULL){ return; }

    std::map<RoleType, float> role_scores;
    role_scores[ATTACKER_ROLE] = attackerScore();
    role_scores[DEFENDER_ROLE] = defenderScore();
    role_scores[MARAUDER_ROLE] = marauderScore();

    RoleAssigner::instance().updatePlayerRoleScores(player_id, role_scores);
    RoleType my_role = ATTACKER_ROLE; //RoleAssigner::instance().getBestRole(player_id);

    switch(my_role){
      case ATTACKER_ROLE:
        if(attacker_role == NULL){ attacker_role = new AttackerRole(pship); }
        if(defender_role != NULL){ delete defender_role; defender_role = NULL; }
        if(marauder_role != NULL){ delete marauder_role; marauder_role = NULL; }
        attacker_role->perform();
        break;

      case DEFENDER_ROLE:
        if(attacker_role != NULL){ delete attacker_role; attacker_role = NULL; }
        if(defender_role == NULL){ defender_role = new DefenderRole(pship); }
        if(marauder_role != NULL){ delete marauder_role; marauder_role = NULL; }
        defender_role->perform();
        break;

      case MARAUDER_ROLE:
        if(attacker_role != NULL){ delete attacker_role; attacker_role = NULL; }
        if(defender_role != NULL){ delete defender_role; defender_role = NULL; }
        if(marauder_role == NULL){ marauder_role = new MarauderRole(pship); }
        marauder_role->perform();
        break;

      default:
        break;
    }
    
    return;
}

float AIPlayer::attackerScore(void){
    Ship *pship = (Ship*)(my_ship_ref.getPointer());
    if(pship == NULL){ return 0.0f; }

    float score = 0.0f;

    if(pship->haveFlag()){ score += 1.0f; } 
    //if(attacker_role != NULL){ score += 0.05f; }

    Team &enemy_team(GameState::instance().getTeam(1-team_id));
    float flag_dist = (pship->getPosition()-enemy_team.getFlag()->getPosition()).length();
    
    score += 1.0f/(flag_dist+1.0f);
   
    return score;
}

float AIPlayer::defenderScore(void){
    return 0.0f;
}

float AIPlayer::marauderScore(void){
    return 0.0f;
}

void AIPlayer::purgeRoles(void){
    if(attacker_role != NULL){ delete attacker_role; attacker_role = NULL; }
    if(defender_role != NULL){ delete defender_role; defender_role = NULL; }
    if(marauder_role != NULL){ delete marauder_role; marauder_role = NULL; }
}

